import api from '../../../api/index'
import CONFIG from '../../../utils/config';
Page({
    data: {
        static: {
            playIcon:'https://hxhealth.oss-cn-beijing.aliyuncs.com/web/lk-webh5/intelligent-recommend/sport/play-icon.png'
        },
        test: 'https://lk-shuzhizhongtai-common.oss-cn-beijing.aliyuncs.com/ai-algorithm-nlp/coverPic/01.png',
    },

    onLoad: function (options) {
        // this.fromHome = options.fromHome // 记录页面来源
        console.log('999');
        console.log(wx.getStorageSync('accessToken'));
        if( wx.getStorageSync('accessToken')) {
            this.getTicket()
        } else {
            this.getorg()
        }
    },
    getorg () {
        const actions = {
          data: {},
          head: {}
        };
        try {
          api.get(`/api/hxhealth/login/orgapp/${CONFIG.orgCode}/${CONFIG.appCode}`,actions,true).then((res) => {
            console.log(res);
            console.log(res.head['Set-Cookie']);
            if (res.head['Set-Cookie']) {
              wx.setStorageSync('accessToken', res.head['Set-Cookie']);
            } 
            this.getTicket()
          });
        } catch (e) {
          console.error(e);
        }
      },
    async getTicket() {
        try {
            // await app.isLogin({ctx: this})
            const list = await api.post('/api/hxhealth/user/laikangticket')
            console.log(list);
            this.getList(list[0].url)
        console.log('haha----------------');
            return list
        } catch (e) {
            return null
        }
    },

    toPlay (e) {
        const item = e.currentTarget.dataset.item
        console.log(item)
        let musicObjStr = JSON.stringify(item)
        const url = `/emotionalRecommendation/pages/home/play/index?music=${encodeURIComponent(
            musicObjStr
        )}`
        wx.navigateTo({
            url
        })
    },
    getList (name) {
        const url = `${name}/api/health/recommend/emotion?`
        api.adminGet(url).then(res => {
            console.log(res, '加载音乐列表')
            const list = JSON.parse(res[0].data).resList
            console.log(list, '音乐列表数据')
            this.setData({
                list
            })
        })
    }
})
